﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace IESN.MyControl
{
    [ToolboxBitmap(@"e:\dms_ico\TRFFC09.ICO")]
    public partial class Arrow : Control
    {
        private IContainer components;
        private Direction mDirection;
        private float msngBorderWidth;
        private AlarmStatus mStatus;
        private float mTextFontSize;
        private string mtextString;

        public event StatusChangedEventHandler StatusChangedEvent;

        public Arrow()
        {
            base.Layout += new LayoutEventHandler(this.Alarm_Layout);
            base.Resize += new EventHandler(this.Alarm_Resize);
            this.mStatus = AlarmStatus.statusGreen;
            this.msngBorderWidth = 1f;
            this.mtextString = "";
            this.mTextFontSize = 10f;
            this.mDirection = Direction.right;
            this.InitializeComponent();
        }

        private void Alarm_Layout(object sender, LayoutEventArgs e)
        {
            string affectedProperty = e.AffectedProperty;
            if (string.Equals(affectedProperty, "Bounds", StringComparison.CurrentCultureIgnoreCase))
            {
                this.Width = this.Height * 1;
            }
            else
            {
                this.Width = this.Height * 1;
            }
        }

        private void Alarm_Resize(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected virtual void DrawLight(AlarmStatus LightToDraw, Graphics grfGraphics)
        {
            //Color orangeRed;
            Color orangeRed = new Color() ;
            //PointF tf;
            //PointF tf2;
            //PointF tf3;
            //PointF tf4;
            PointF tf=new PointF();
            PointF tf2 = new PointF();
            PointF tf3 = new PointF();
            PointF tf4 = new PointF();
            switch (LightToDraw)
            {
                case AlarmStatus.statusRed:
                    orangeRed = Color.OrangeRed;
                    break;

                case AlarmStatus.statusYellow:
                    orangeRed = Color.Yellow;
                    break;

                case AlarmStatus.statusGreen:
                    orangeRed = Color.LimeGreen;
                    break;
            }
            Brush brush = new SolidBrush(orangeRed);
            switch (this.JTDirection)
            {
                case Direction.left:
                    tf4 = new PointF(0.01f, Convert.ToSingle((double) (((double) this.Height) / 2.0)));
                    tf = tf4;
                    tf4 = new PointF(Convert.ToSingle((double) (this.Width * 0.98)), Convert.ToSingle((double) (this.Height * 0.98)));
                    tf2 = tf4;
                    tf4 = new PointF(Convert.ToSingle((double) (this.Width * 0.98)), 0.01f);
                    tf3 = tf4;
                    break;

                case Direction.right:
                    tf4 = new PointF(0.01f, 0.01f);
                    tf = tf4;
                    tf4 = new PointF(0.01f, Convert.ToSingle((double) (this.Height * 0.98)));
                    tf2 = tf4;
                    tf4 = new PointF(Convert.ToSingle((double) (this.Width * 0.98)), Convert.ToSingle((double) (((double) this.Height) / 2.0)));
                    tf3 = tf4;
                    break;

                case Direction.up:
                    tf4 = new PointF(Convert.ToSingle((double) (((double) this.Width) / 2.0)), 0.01f);
                    tf = tf4;
                    tf4 = new PointF(0.01f, Convert.ToSingle((double) (this.Height * 0.98)));
                    tf2 = tf4;
                    tf4 = new PointF(Convert.ToSingle((double) (this.Width * 0.98)), Convert.ToSingle((double) (this.Height * 0.98)));
                    tf3 = tf4;
                    break;

                case Direction.down:
                    tf4 = new PointF(0.01f, 0.01f);
                    tf = tf4;
                    tf4 = new PointF(Convert.ToSingle((double) (this.Width * 0.98)), 0.01f);
                    tf2 = tf4;
                    tf4 = new PointF(Convert.ToSingle((double) (((double) this.Width) / 2.0)), Convert.ToSingle((double) (this.Height * 0.98)));
                    tf3 = tf4;
                    break;
            }
            PointF[] points = new PointF[] { tf, tf2, tf3 };
            FillMode winding = FillMode.Winding;
            grfGraphics.FillPolygon(brush, points, winding);
            brush.Dispose();
        }

        //[DebuggerStepThrough]
        private void InitializeComponent()
        {
            this.Name = "UserControl1";
            Size size = new Size(0x128, 0x128);
            this.Size = size;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Graphics grfGraphics = pe.Graphics;
            this.DrawLight(this.Status, grfGraphics);
            Font font = new Font("Times New Roman", this.TextFontSize, FontStyle.Regular);
            Brush black = Brushes.Black;
            SizeF ef = grfGraphics.MeasureString(this.TextString, font);
            PointF point = new PointF((float) Convert.ToInt16((double) ((((double) this.Width) / 2.0) - (ef.Width / 2f))), (float) Convert.ToInt16((double) ((((double) this.Height) / 2.0) - (ef.Height / 2f))));
            grfGraphics.DrawString(this.TextString, font, black, point);
            grfGraphics.Dispose();
        }

        public void ResetStatus()
        {
            this.Status = AlarmStatus.statusGreen;
        }

        public bool ShouldSerializeStatus()
        {
            if (this.mStatus == AlarmStatus.statusGreen)
            {
                return false;
            }
            return true;
        }

        [DefaultValue((float) 1f), Description("周围边框的宽度")]
        public float BorderWidth
        {
            get
            {
                return this.msngBorderWidth;
            }
            set
            {
                if (this.msngBorderWidth != value)
                {
                    this.msngBorderWidth = value;
                    this.Invalidate();
                }
            }
        }

        public Direction JTDirection
        {
            get
            {
                return this.mDirection;
            }
            set
            {
                if (this.mDirection != value)
                {
                    this.mDirection = value;
                    this.Invalidate();
                }
            }
        }

        [Description("状态（颜色）")]
        public AlarmStatus Status
        {
            get
            {
                return this.mStatus;
            }
            set
            {
                if (this.mStatus != value)
                {
                    this.mStatus = value;
                    if (this.StatusChangedEvent != null)
                    {
                        this.StatusChangedEvent(this.mStatus);
                    }
                    this.Invalidate();
                }
            }
        }

        public float TextFontSize
        {
            get
            {
                return this.mTextFontSize;
            }
            set
            {
                if (this.mTextFontSize != value)
                {
                    this.mTextFontSize = value;
                    this.Invalidate();
                }
            }
        }

        public string TextString
        {
            get
            {
                return this.mtextString;
            }
            set
            {
                if (string.Equals(this.mtextString, value, StringComparison.CurrentCultureIgnoreCase))
                {
                    this.mtextString = value;
                    this.Invalidate();
                }
            }
        }

        public enum AlarmStatus
        {
            statusGreen = 3,
            statusRed = 1,
            statusYellow = 2
        }

        public enum Direction
        {
            down = 4,
            left = 1,
            right = 2,
            up = 3
        }

        public delegate void StatusChangedEventHandler(AlarmStatus NewStatus);
    }
}
